#include "stdafx.h"
#include "shape_manage.h"
vector<string> files = vector<string>();

void loadShapes(void) {
	string dir = string("../../shapes/");	
	
	// TODO: use directory listing
	//files.push_back("shape-1.txt");
	/*files.push_back("shape-2.txt");
	files.push_back("shape-3.txt");
	files.push_back("shape-4.txt");
	files.push_back("shape-5.txt");
	files.push_back("shape-6.txt");
	files.push_back("shape-7.txt");
	files.push_back("shape-8.txt");*/

	//getdir(dir,files);

	WIN32_FIND_DATA ffd;
	HANDLE hFind;
  

	// Find the first file in the directory.
	hFind = FindFirstFile(TEXT("..\\..\\shapes\\*"), &ffd);
	files.push_back(ffd.cFileName);

	// List all the other files in the directory.

	while (FindNextFile(hFind, &ffd) != 0) 
	{
		files.push_back(ffd.cFileName);
	}



	for (unsigned int i = 0; i < files.size(); i++) {
		// only look for txt files
		if (files[i].find(".txt", 0) != string::npos) {

			string filename = dir + files[i];
			cout << filename << endl;
			ifstream file;
			file.open(filename.c_str());
			if (!file) {
				printf("Unable to open file %s\n", filename.c_str());
				return;
			}

			vector<float*> outline = vector<float*>();
			vector<float*> required = vector<float*>();

			bool isProcessingOutline = true;

			while (!file.eof()) {
				float x, y;
				file >> x >> y;
				file.get(); // swallow newline

				float* coordinate = (float*) malloc(sizeof(float) * 3);
				coordinate[0] = x;
				coordinate[1] = y;
				coordinate[2] = 0.0f;

				if (isProcessingOutline) {
					outline.push_back(coordinate);
				}
				else {
					required.push_back(coordinate);
				}

				cout << x << ", " << y << endl;
				if (file.peek() == '#') {
					// separator between outline points and required points
					file.get();
					isProcessingOutline = false;
				}
			}
			file.close();

			Shape* shape = new Shape();

			shape->setOutline(outline);
			shape->setRequired(required);

			shapes.push_back(shape);
		}
	}
}